using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.ServiceManager
{
    public class SportCupManager
    {
        #region CONSTRUCTORS
        static SportCupManager()
        {
            _data = new SportCupData(Util.GetConnectionString("SMSSERVICE"));
            _error = "";
        }
        #endregion

        #region ATTRIBUTES
        private static SportCupData _data;
        private static string _error;
        #endregion

        #region PROPERTIES
        public static string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        #endregion

        #region METHODS
        public static bool Insert(string cupName, string cupCode, string cupCodeOpen, byte type)
        {
            _error = "";
            try
            {
                object[] values = new object[4];
                values[0] = Util.ReplaceNullTo(cupName, DBNull.Value);
                values[1] = Util.ReplaceNullTo(cupCode, DBNull.Value); ;
                values[2] = Util.ReplaceNullTo(cupCodeOpen, DBNull.Value);
                values[3] = Util.ReplaceNullTo(type, DBNull.Value);

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Insert(SportCupInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[4];
                values[0] = Util.ReplaceNullTo(info.CupName, DBNull.Value);
                values[1] = Util.ReplaceNullTo(info.CupCode, DBNull.Value);
                values[2] = Util.ReplaceNullTo(info.CupCodeOpen, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.CupType, DBNull.Value);

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(int cupID, string cupName, string cupCode, string cupCodeOpen, byte type)
        {
            _error = "";
            try
            {
                object[] values = new object[5];
                values[0] = cupID;
                values[1] = Util.ReplaceNullTo(cupName, DBNull.Value);
                values[2] = Util.ReplaceNullTo(cupCode, DBNull.Value); ;
                values[3] = Util.ReplaceNullTo(cupCodeOpen, DBNull.Value);
                values[4] = Util.ReplaceNullTo(type, DBNull.Value);

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(SportCupInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[5];
                values[0] = info.CupID;
                values[1] = Util.ReplaceNullTo(info.CupName, DBNull.Value);
                values[2] = Util.ReplaceNullTo(info.CupCode, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.CupCodeOpen, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.CupType, DBNull.Value);

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(int cupID)
        {
            _error = "";
            try
            {
                object[] values = { cupID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(SportCupInfo info)
        {
            _error = "";
            try
            {
                object[] values = { info.CupID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static SportCupInfo Select(int cupID)
        {
            _error = "";
            try
            {
                object[] values = { cupID };
                DataTable dTable = _data.Select(values);
                if (dTable.Rows.Count < 1) return null;


                SportCupInfo info = new SportCupInfo(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll()
        {
            _error = "";
            try
            {
                return _data.SelectAll();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAllForRelate(byte bytStatus)
        {
            _error = "";
            try
            {
                return _data.SelectAllForRelate(bytStatus);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectCupAtLocation(string strLoc, byte bytStatus)
        {
            _error = "";
            try
            {
                object[] values = new object[2];
                values[0] = strLoc;
                values[1] = bytStatus;
                return _data.SelectCupAtLocation(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectCupAtTip()
        {
            _error = "";
            try
            {
                object[] values = new object[0];
                return _data.SelectCupAtTip(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectCupConfig(int intCountryID)
        {
            _error = "";
            try
            {
                object[] values = new object[1];
                values[0] = intCountryID;
                return _data.SelectCupConfig(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectRelateConfig(int intCupID)
        {
            _error = "";
            try
            {
                object[] values = new object[1];
                values[0] = intCupID;
                return _data.SelectRelateConfig(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectCupAtHome()
        {
            _error = "";
            try
            {
                object[] values = new object[0];
                return _data.SelectCupAtHome(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        #endregion

    }
}